<!-- 元数据编辑对话框 -->
<template>
  <CommonDialog v-model="visible" :loading="loading" title="发布模型" :width="500" @submit="submit" @cancel="cancel">
    <Form ref="formEl" :model="formData" :label-width="100">
      <FormItem label="发布选项">
        <RadioGroup v-model="formData.isRecover">
          <Radio v-for="item in MODELTYPE_PUSH_OPTIONS" :key="item.value" :label="item.value">
            {{ item.label }}</Radio>
        </RadioGroup>
      </FormItem>

      <FormItem label="版本号递增">
        <RadioGroup v-model="formData.isdataversionadd">
          <Radio label="true">是</Radio>
          <Radio label="false">否</Radio>
        </RadioGroup>
      </FormItem>

      <FormItem label="版本号">
        <Input v-model="formData.dataversion" placeholder="指定版本号 (特殊用法)" />
      </FormItem>
    </Form>
  </CommonDialog>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { Message } from 'view-ui-plus'

import { modelManage } from '@/api/modules/modelSetting'

import { MODELTYPE_PUSH_OPTIONS } from '@/util/conf'

const emit = defineEmits(['cancel', 'submit'])

// 是否打开弹窗
const visible = ref(true)

// 加载状态
const loading = ref(false)

const dataId = ref(undefined)
// 表单元素ref
const formEl = ref(null)
// 表单数据
const formData = ref({
  isRecover: 'yes', //名称
  isdataversionadd: 'true',
  dataversion: ''
})

/**
 * 提交表单
 */
const submit = async () => {
  let valid = await formEl.value.validate()
  if (!valid) return

  loading.value = true

  try {
    const params = {
      ...formData.value,
      id: dataId.value
    }

    await modelManage.publish({
      data: params
    })

    Message.success('操作成功')
    emit('submit')
  } catch (error) {
    console.error(error)
  }

  loading.value = false
}

/**
 * 取消表单
 */
const cancel = () => {
  visible.value = false
  emit('cancel')
}

/**
 * 初始化对话框
 */
const initDialog = id => {
  // 显示对话框
  visible.value = true

  dataId.value = id
}

defineExpose({
  initDialog
})

onMounted(() => {})
</script>

<style scoped lang="scss">
.flex-form {
  display: flex;
}
.ivu-input-wrapper,
.ivu-select,
.el-select {
  width: 240px;
}
::deep(.ivu-form-item-label) {
  text-align: right;
}
</style>